package com.lry.jvm.instructions.stores;

import com.lry.jvm.instructions.base.NoOperandsInstruction;
import com.lry.jvm.rtda.Frame;
import com.lry.jvm.rtda.JObject;
import com.lry.jvm.rtda.OperandStack;
import com.lry.jvm.util.Util;

//[i] = ref,给引用类型数组赋值
public class aastore extends NoOperandsInstruction {
    @Override
    public byte getOpCode() {
        return 0x53;
    }

    @Override
    public void execute(Frame frame) throws Exception {
        OperandStack stack = frame.getOperandStack();
        //
        JObject ref = stack.popRef();

        int index = stack.popInt();
        JObject arrRef = stack.popRef();
        if(null==arrRef){
            Util.panic("java.lang.NullPointerException");
        }

        JObject[] refs = arrRef.toRefs();
        if(index<0||index>=refs.length){
            Util.panic("ArrayIndexOutOfBoundsException");
        }
        refs[index] = ref;

    }
}
